using UnityEngine;
using UnityEngine.EventSystems;

public class DropSwapItem : MonoBehaviour, IDropHandler, IEventSystemHandler
{
	public GUIItemCollector GUIItem;

	public void Start()
	{
		if (GUIItem == null)
		{
			GUIItem = GetComponent<GUIItemCollector>();
		}
	}

	public void OnDrop(PointerEventData data)
	{
		if (GUIItem == null || GUIItem.Item == null)
		{
			return;
		}
		GUIItemEquipped itemEquipDrop = GetItemEquipDrop(data);
		if (itemEquipDrop != null && GUIItem.Type == "Inventory")
		{
			if (!UnitZ.playerManager.PlayingCharacter.inventory.EquipItemToStickerByCollector(GUIItem.Item, itemEquipDrop.Tag))
			{
				UnitZ.playerManager.PlayingCharacter.inventory.UnEquipItem(itemEquipDrop.CurrentItemCollector);
			}
			return;
		}
		GUIItemCollector itemCollectorDrop = GetItemCollectorDrop(data);
		if (!(itemCollectorDrop != null) || itemCollectorDrop.Item == null)
		{
			return;
		}
		if (itemCollectorDrop.Type == "Ground" && GUIItem.Type == "Inventory")
		{
			if (itemCollectorDrop.Item != null)
			{
				UnitZ.playerManager.PlayingCharacter.Interactive(itemCollectorDrop.Item.Item.gameObject);
			}
		}
		else if (itemCollectorDrop.Type == "Inventory" && GUIItem.Type == "Ground")
		{
			if (itemCollectorDrop.Item != null)
			{
				UnitZ.playerManager.PlayingCharacter.inventory.DropItemByCollector(itemCollectorDrop.Item, itemCollectorDrop.Item.Num);
			}
		}
		else
		{
			if (!GUIItem.currentInventory)
			{
				return;
			}
			ItemCollector item = new ItemCollector();
			GUIItem.currentInventory.CopyCollector(item, GUIItem.Item);
			if ((!(GUIItem.Type == "Stock") && !(GUIItem.Type == "Inventory")) || !(itemCollectorDrop.Type != "Shop"))
			{
				return;
			}
			if (GUIItem.currentInventory != itemCollectorDrop.currentInventory)
			{
				if ((bool)GUIItem.currentInventory.character && GUIItem.currentInventory.character.IsMine)
				{
					UnitZ.playerManager.PlayingCharacter.inventory.PutCollector(itemCollectorDrop.Item, GUIItem.Item.InventoryIndex);
					UnitZ.playerManager.PlayingCharacter.inventory.PutCollectorSync(item, itemCollectorDrop.Item.InventoryIndex);
				}
				else
				{
					UnitZ.playerManager.PlayingCharacter.inventory.PutCollectorSync(itemCollectorDrop.Item, GUIItem.Item.InventoryIndex);
					UnitZ.playerManager.PlayingCharacter.inventory.PutCollector(item, itemCollectorDrop.Item.InventoryIndex);
				}
			}
			else if ((bool)GUIItem.currentInventory.character && GUIItem.currentInventory.character.IsMine)
			{
				GUIItem.currentInventory.SwarpCollector(GUIItem.Item, itemCollectorDrop.Item);
			}
			else
			{
				UnitZ.playerManager.PlayingCharacter.inventory.PutCollectorSync(itemCollectorDrop.Item, GUIItem.Item.InventoryIndex);
				UnitZ.playerManager.PlayingCharacter.inventory.PutCollectorSync(item, itemCollectorDrop.Item.InventoryIndex);
			}
		}
	}

	private GUIItemEquipped GetItemEquipDrop(PointerEventData data)
	{
		GameObject pointerDrag = data.pointerDrag;
		if (pointerDrag == null)
		{
			return null;
		}
		if ((bool)pointerDrag.GetComponent<GUIItemEquipped>())
		{
			return pointerDrag.GetComponent<GUIItemEquipped>();
		}
		return null;
	}

	private GUIItemCollector GetItemCollectorDrop(PointerEventData data)
	{
		GameObject pointerDrag = data.pointerDrag;
		if (pointerDrag == null)
		{
			return null;
		}
		if ((bool)pointerDrag.GetComponent<DragShortcut>())
		{
			return null;
		}
		if ((bool)pointerDrag.GetComponent<GUIItemCollector>())
		{
			return pointerDrag.GetComponent<GUIItemCollector>();
		}
		return null;
	}
}
